#! /usr/bin/env bash

cd "$(dirname "$0")"
cd ..

cp /dev/null database/dictionary-64.txt
cp /dev/null database/dictionary-128.txt
cp /dev/null database/dictionary-152.txt
cp /dev/null database/dictionary-256.txt

if ls capture/*/key.txt &> /dev/null
then
	for file in capture/*/key.txt
	do
		key=$(cat $file | sed 's/\(..\)/&:/g;s/:$//')
		case ${#key} in
			14 )
				echo $key >> database/dictionary-64.txt
			;;
			38 )
				echo $key >> database/dictionary-128.txt
			;;
			47 )
				echo $key >> database/dictionary-152.txt
			;;
			86 )
				echo $key >> database/dictionary-256.txt
			;;
		esac
	done
fi

if [ -f database/dictionary.txt ]
then
	while read key
	do
		key=$(echo $key | sed 's/\(..\)/&:/g;s/:$//')
		case ${#key} in
			14 )
				echo $key >> database/dictionary-64.txt
			;;
			38 )
				echo $key >> database/dictionary-128.txt
			;;
			47 )
				echo $key >> database/dictionary-152.txt
			;;
			86 )
				echo $key >> database/dictionary-256.txt
			;;
		esac
	done < database/dictionary.txt
fi

if [ -f database/key.txt ]
then
	while read key
	do
		eval key=($key)
		key=$(echo ${key[2]} | sed 's/\(..\)/&:/g;s/:$//')
		case ${#key} in
			14 )
				echo $key >> database/dictionary-64.txt
			;;
			38 )
				echo $key >> database/dictionary-128.txt
			;;
			47 )
				echo $key >> database/dictionary-152.txt
			;;
			86 )
				echo $key >> database/dictionary-256.txt
			;;
		esac
	done < database/key.txt
fi

sort database/dictionary-64.txt > database/dictionary-64-sort.txt
uniq database/dictionary-64-sort.txt > database/dictionary-64.txt
rm database/dictionary-64-sort.txt

sort database/dictionary-128.txt > database/dictionary-128-sort.txt
uniq database/dictionary-128-sort.txt > database/dictionary-128.txt
rm database/dictionary-128-sort.txt

sort database/dictionary-152.txt > database/dictionary-152-sort.txt
uniq database/dictionary-152-sort.txt > database/dictionary-152.txt
rm database/dictionary-152-sort.txt

sort database/dictionary-256.txt > database/dictionary-256-sort.txt
uniq database/dictionary-256-sort.txt > database/dictionary-256.txt
rm database/dictionary-256-sort.txt

